﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Windows.Forms;

namespace DC.Utils
{
    public class Database
    {
        public Database(ServerProperty serverProperty)
        {
            this.connstr = string.Format("server={0};uid={1};pwd={2};database={3}",
                serverProperty.Host,
                serverProperty.User,
                serverProperty.Password,
                serverProperty.DatabaseName);
            conn = new System.Data.SqlClient.SqlConnection(connstr);
            try
            {
                conn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("连接超时，请检查服务器配置", "连接错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Views.Form_Settings settings = new Views.Form_Settings();
                settings.isfirst = false;
                settings.ShowDialog();
            }
        }

        System.Data.SqlClient.SqlConnection conn; //全局的连接对象


        private string connstr = string.Empty;
        public int Execute(string NonQuerySql, bool DebugFlag)    //执行一个非查询的数据库操作
        {
            string sql = NonQuerySql.ToLower();
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = NonQuerySql;
            int n = 0;
            if ((sql.IndexOf("update ") >= 0 || sql.IndexOf("delete ") >= 0) && sql.IndexOf(" where ") < 0)//无条件的删除和修改不允许
                throw new Exception(NonQuerySql + "\nupdate或delete时没有where条件");
            try
            {
                if (DebugFlag) { conn.Close(); throw new Exception(NonQuerySql); }
                n = cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(NonQuerySql + "\n\n" + e.ToString()); ;
            }
            return n;
        }
        public int Execute(string NonQuerySql)    //执行一个非查询的数据库操作
        {
            return Execute(NonQuerySql, false);
        }
        public DataSet GetDs(string QuerySql)    //执行一个查询的数据库操作，返回数据集
        {
            string sql1 = QuerySql.Trim().ToLower();
            if (sql1.StartsWith("insert ") || sql1.StartsWith("update ") || sql1.StartsWith("delete "))
                throw new Exception(QuerySql + "\n本方法仅用于查询");
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connstr);
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = QuerySql;
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            System.Data.DataSet ds = new System.Data.DataSet();

            try
            {
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw new Exception(QuerySql + "\n\n" + e.ToString()); ;
            }
            if (ds == null) return null;
            return ds;
        }
        public object GetData(string QuerySql)    //执行一个查询的数据库操作，返回一个值
        {
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = QuerySql;
            object result = null;
            try
            {
                result = (object)cmd.ExecuteScalar();
            }
            catch (Exception e)
            {
                throw new Exception(QuerySql + "\n\n" + e.ToString()); ;
            }

            return result;
        }

        public void Dispose()
        {
            this.conn.Close();
            this.conn.Dispose();
        }
    }
}